package com.simple.piger.controller.company;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.simple.piger.component.CommonComponent;
import com.simple.piger.dto.req.InviteCompanyJobReqDto;
import com.simple.piger.dto.req.JobTitleReqDto;
import com.simple.piger.entity.*;
import com.simple.piger.service.*;
import com.simple.piger.utils.AjaxResult;
import com.simple.piger.utils.PublicUtil;
import com.simple.piger.controller.BaseController;
import com.simple.piger.dto.resp.company.InviteCompanyJobRespDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 *  专门处理岗位请求的接口，比如查询，删除，更改岗位信息
 * </p>
 *
 * @author baolw
 * @since 2020-02-06
 */
@RestController
@RequestMapping("/inviteCompanyJob")
public class InviteCompanyJobController extends BaseController {
    @Autowired
    private InviteCompanyJobService inviteCompanyJobService;
    @Autowired
    private InviteCompanyEmpService inviteCompanyEmpService;
    @Autowired
    private ApplyUserService applyUserService;
    @Autowired
    private InviteCompanyService inviteCompanyService;
    @Autowired
    private CommonComponent commonComponent;
    @Autowired
    private JobTitleService jobTitleService;

    /**
     * 列出岗位信息
     * @param hashMap
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam HashMap hashMap) {
        JSONObject jsonObject = PublicUtil.getJSONObject(hashMap);
        String name = jsonObject.getString("name");
        String companyId = commonComponent.getCompanyIdByCmsUserId(getValueFromSession("userId"));
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        List<InviteCompanyJobRespDto> list = inviteCompanyJobService.list(page, limit, name, companyId);
        int count = inviteCompanyJobService.count(name,companyId);
        return success(count, list);
    }

    /**
     * 首页查询接口，返回岗位信息
     * @param hashMap
     * @return
     */
    @GetMapping("/homePage/list")
    public AjaxResult homePageList(@RequestParam HashMap hashMap) {
        JSONObject jsonObject = PublicUtil.getJSONObject(hashMap);
        String name = jsonObject.getString("name");
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        List<InviteCompanyJobRespDto> list = inviteCompanyJobService.homePageList(page, limit, name);
        int count = inviteCompanyJobService.homePageListCount(name);
        return success(count, list);
    }

    /**
     * 返回岗位信息的接口
     * @param companyEmpId
     * @return
     */
    @GetMapping("/detail/{companyEmpId}")
    public AjaxResult detail(@PathVariable("companyEmpId") String companyEmpId) {
        InviteCompanyJob inviteCompanyJob = inviteCompanyJobService.selectById(companyEmpId);
        InviteCompanyJobRespDto dto = new InviteCompanyJobRespDto();
        BeanUtils.copyProperties(inviteCompanyJob, dto);
        InviteCompany company = inviteCompanyService.selectById(inviteCompanyJob.getCompanyId());
        JobTitle jobTitle = jobTitleService.selectById(inviteCompanyJob.getJobTitle());
        dto.setParentId(jobTitle.getParentId());
        dto.setCompanyName(company.getCompanyName());
        return success(dto);
    }

    /**
     * 保存或者更新岗位信息的接口
     * @param hashMap
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestParam HashMap hashMap) {
        JSONObject jsonObject = PublicUtil.getJSONObject(hashMap);
        InviteCompanyJobReqDto reqDto = JSONObject.toJavaObject(jsonObject, InviteCompanyJobReqDto.class);
        String userId = getValueFromSession("userId");
        EntityWrapper<ApplyUser> ew = new EntityWrapper<>();
        ew.eq("cms_sys_user_id", userId);
        ApplyUser applyUser = applyUserService.selectOne(ew);
        EntityWrapper<InviteCompanyEmp> empEw = new EntityWrapper<>();
        empEw.eq("apply_user_id", applyUser.getId());
        InviteCompanyEmp emp = inviteCompanyEmpService.selectOne(empEw);
        reqDto.setCompanyId(emp.getInviteCompanyId());
        reqDto.setApplyUserId(applyUser.getId());
        inviteCompanyJobService.saveOrUpdate(reqDto);
        return success();
    }

    /**
     * 删除岗位信息的接口
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") String id) {
        InviteCompanyJob job = inviteCompanyJobService.selectById(id);
        job.setDeleteFlag("1");
        job.setUpdateTime(new Date());
        job.updateById();
        return success();
    }

    /**
     * 更新岗位信息状态的接口
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/updateStatus")
    public AjaxResult updateStatus(@PathVariable("id") String id,  @RequestParam("status") String status) {
        InviteCompanyJob job = inviteCompanyJobService.selectById(id);
        job.setUpdateTime(new Date());
        job.setStatus(status);
        job.updateById();
        return success();
    }
}
